{% load geonode_client_tags %}

{% client_viewer_js	%}
<style>
	#content {
		position: relative;
	}
	#map {
		height: 600px;
	}
</style>

<script type="text/javascript">
	document.addEventListener('DOMContentLoaded', function () {
    var defaultConfig = {
        authStatus: {% if user.is_authenticated %} 200{% else %} 401{% endif %},
        proxy: '{{ PROXY_URL }}',
        {% if MAPFISH_PRINT_ENABLED %}
        printService: "{{GEOSERVER_BASE_URL}}pdf/",
        {% else %}
        printService: "",
        {% endif %}
        /* The URL to a REST map configuration service.  This service 
         * provides listing and, with an authenticated user, saving of 
         * maps on the server for sharing and editing.
         */
        rest: "{% url "maps_browse" %}",
        ajaxLoginUrl: "{% url "account_ajax_login" %}",
        homeUrl: "{% url "home" %}",
        portalItems: [{
            xtype: "container",
            layout: "fit",
            height: 81,
            region: "north"
        }],
        localGeoServerBaseUrl: "{{ GEOSERVER_BASE_URL }}",
        localCSWBaseUrl: "{{ CATALOGUE_BASE_URL }}",
        csrfToken: "{{ csrf_token }}",
        tools: [{ptype: "gxp_getfeedfeatureinfo"}],
		};
		var config = Object.assign( defaultConfig, {{ viewer|safe }});
    var options = {mapConfig: config, zoomToLayer: true};
    if (config.proxy !== '') {
      options.proxy = "{{PROXY_URL}}";
    }
    if ("{{ MAP_CLIENT_USE_CROSS_ORIGIN_CREDENTIALS }}" == 'True') {
      var checkUrl = "{{ resource.service.base_url }}";
      checkUrlCORSCompatible(checkUrl).then(function(response) {
        options.crossOriginCredentials = true;
      }).catch(function(err) {
        console.log("Service {{ resource.service }} is not CORS compatible. Status error: ", err.statusText);
        console.log("Therefore, making requests anonymously instead of with credentials");
        options.crossOriginCredentials = false;
      });
    } else {
      options.crossOriginCredentials = false;
    }
    var viewer = new window.Viewer('preview_map', options);
    viewer.view();
});
</script>
